package com.decent.manager.system.dao;

import com.decent.manager.system.dao.provider.SystemGoodsDaoProvider;
import com.decent.manager.system.dto.SystemGoodsDTO;
import com.decent.manager.system.vo.SystemGoodsVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: decent Gjh
 * @date: 2022/10/25/19:01
 * @description:
 */
@Mapper
public interface SystemGoodsDao {
    /**
     * 插入商品
     * @param goodsDTO 商品信息
     */
    @Insert("insert into common_goods (name, brand_id, code, introduce, image_url, present_price," +
            "original_price, stock, create_datetime) " +
            " values (#{goods.name},#{goods.brandId},#{goods.code},#{goods.introduce},#{goods.imageUrl},#{goods.presentPrice}," +
            "#{goods.originalPrice},#{goods.stock},now()) ")
    void insert(@Param("goods") SystemGoodsDTO goodsDTO);

    /**
     * 根据id删除商品
     * @param goodsId 要删除的商品id
     * @return 影响行数
     */
    @Delete("delete from common_goods where id = #{goodsId}")
    int deleteGoods(@Param("goodsId") int goodsId);

    /**
     * 根据id修改商品
     * @param goodsDTO 商品信息
     * @return 影响行数
     */
    @Update("update common_goods set name = #{goods.name}, brand_id = #{goods.brandId}, " +
            "code = #{goods.code}, introduce = #{goods.introduce}, " +
            "present_price = #{goods.presentPrice}, original_price = #{goods.originalPrice}, " +
            "stock = #{goods.stock}, update_datetime = now() where id = #{goods.id}")
    int updateGoods(@Param("goods") SystemGoodsDTO goodsDTO);

    /**
     * 查询商品
     * @param goodsDTO 过滤信息
     * @return 商品列表
     */
    @SelectProvider(type = SystemGoodsDaoProvider.class, method = "list")
    List<SystemGoodsVO> selectGoods(@Param("goods") SystemGoodsDTO goodsDTO);

    /**
     * 根据品牌id ，查询该品牌下的商品个数
     * @param brandId 商品id
     * @return 商品个数
     */
    @Select("select count(*) from common_goods where brand_id = #{brandId}")
    int selectNumberByBrandId(@Param("brandId") int brandId );

    /**
     * 更新库存数量
     * @param code 商品代码
     * @param stock 库存数
     * @return 影响行数
     */
    @Update("update common_goods set stock = #{stock} where code = #{code}")
    int updateStockNumByCode(@Param("code") String code, @Param("stock")int stock);

    /**
     * 查询橙券代码列表中是否有此代码
     * @param code 橙券商品代码
     * @return 数量
     */
    @Select("select count(*) from cheng_code where code = #{code}")
    int verificationCode(@Param("code") String code);
}
